package com.ruoyi.message.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.company.domain.CompanyOther;
import com.ruoyi.company.service.ICompanyInvoiceService;
import com.ruoyi.company.service.ICompanyOtherServer;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.message.service.IMessageService;
import com.ruoyi.sale.domain.SaleorderOrder;
import com.ruoyi.system.service.ISysUserService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangChangShuo
 * @date 22.4.22 15点59分
 */

@Controller
@RequestMapping("/business/message")
public class MessageController extends BaseController {

    @Autowired
    IMessageService iMessageService;

    @Autowired
    ISysUserService iSysUserService;

    @Autowired
    ICompanyOtherServer iCompanyOtherServer;

    @Autowired
    ICompanyInvoiceService iCompanyInvoiceService;

    /**
     * 我的未读消息界面
     */
    @RequiresPermissions("business:message:view")
    @GetMapping("")
    public String list() {
        return "message/list";
    }

    /**
     * 查询 合作单位 列表 只展示当前登陆人的合作单位
     */

    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "title", required = false) String title) {
        startPage();
        //登录人的companyId
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long companyId = sysUser.getCompanyId();
        Map<String, Object> map = new HashMap();
        map.put("companyId", companyId);
        map.put("deptName", title);
        List<CompanyOther> list = iMessageService.selectCompanyOtherMessageList(map);
        return getDataTable(list);
    }

    /**
     * 删除操作
     */
    @GetMapping("/deleteMessage/{deptId}")
    public String deleteMessage(@PathVariable("deptId") String deptId) {

        return null;
    }

    /**
     * 同意操作
     */
    @Transactional
    @Log(title = "同意合作单位申请", businessType = BusinessType.UPDATE)
    @GetMapping("/agreeMessage/{id}")
    @ResponseBody
    public int agreeMessage(@PathVariable("id") Long id) {

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        CompanyOther data = iCompanyOtherServer.selectCompanyOtherById(id);
        Long deptId = data.getDeptId();
        //更新 状态 改为已同意
        //获取登陆人的 companyid
        Long companyId = sysUser.getCompanyId();
        CompanyOther companyOther = new CompanyOther();
        companyOther.setId(data.getId());
        companyOther.setDeptId(data.getDeptId());
        companyOther.setCompanyId(companyId);
        companyOther.setGradeStatus("2");
        companyOther.setAtStatus("2");
        //同意了 便是同意 自己公司 与 传过来的 deptid公司 成为 合作关系  再插入 进other表
        //other表中的deptid则是 自己公司的companyid
        //other表中的companyid则是 穿过来的deptId
        //先查询 在companyother表中有没有这个信息 没有的话再插入
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("companyId", deptId);
        hashMap.put("deptId", companyId);
        hashMap.put("gradeStatus", "1");
        CompanyOther companyOther2 = iCompanyOtherServer.selectByCompanyIdAndDeptId(hashMap);

        if (companyOther2 == null) {
            CompanyOther companyOther1 = new CompanyOther();
            companyOther1.setCompanyId(deptId);
            companyOther1.setDeptId(companyId);
            companyOther1.setCreateBy(sysUser.getNickName());
            companyOther1.setGradeStatus("2");
            companyOther1.setAtStatus("2");
            int i = iCompanyOtherServer.insertCompanyOther(companyOther1);
            int updateCompanyother = iCompanyOtherServer.updateCompanyother(companyOther);
            if (i > 0 & updateCompanyother > 0) {
                return 1;
            } else {
                return 0;
            }
        } else {
            int updateCompanyother = iCompanyOtherServer.updateCompanyother(companyOther);
            if (updateCompanyother > 0) {
                return 1;
            } else {
                return 0;
            }
        }

    }

    /**
     * 一键同意 驳回操作
     */
    @Transactional
    @Log(title = "同意合作单位申请", businessType = BusinessType.UPDATE)
    @GetMapping("/allAgree")
    @ResponseBody
    public int allAgree(@RequestParam(value = "messageList", required = false) List<Long> messageList,
                        @RequestParam(value = "type", required = false) String type) {
        //1代表一键同意
        int i = 0;
        for (int a = 0; a < messageList.size(); a++) {
            if (type.equals("1")) {
                SysUser sysUser = SecurityUtils.getLoginUser().getUser();
                CompanyOther data = iCompanyOtherServer.selectCompanyOtherById(messageList.get(a));
                Long deptId = data.getDeptId();
                //更新 状态 改为已同意
                //获取登陆人的 companyid
                Long companyId = sysUser.getCompanyId();
                CompanyOther companyOther = new CompanyOther();
                companyOther.setId(data.getId());
                companyOther.setDeptId(data.getDeptId());
                companyOther.setCompanyId(companyId);
                companyOther.setGradeStatus("2");
                companyOther.setAtStatus("2");
                //同意了 便是同意 自己公司 与 传过来的 deptid公司 成为 合作关系  再插入 进other表
                //other表中的deptid则是 自己公司的companyid
                //other表中的companyid则是 穿过来的deptId
                //先查询 在companyother表中有没有这个信息 没有的话再插入
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("companyId", deptId);
                hashMap.put("deptId", companyId);
                hashMap.put("gradeStatus", "1");
                CompanyOther companyOther2 = iCompanyOtherServer.selectByCompanyIdAndDeptId(hashMap);

                if (companyOther2 == null) {
                    CompanyOther companyOther1 = new CompanyOther();
                    companyOther1.setCompanyId(deptId);
                    companyOther1.setDeptId(companyId);
                    companyOther1.setCreateBy(sysUser.getNickName());
                    companyOther1.setGradeStatus("2");
                    companyOther1.setAtStatus("2");
                    i = iCompanyOtherServer.insertCompanyOther(companyOther1);
                    int updateCompanyother = iCompanyOtherServer.updateCompanyother(companyOther);
                    i=1;

                } else {
                    int updateCompanyother = iCompanyOtherServer.updateCompanyother(companyOther);
                    i=1;

                }
            }
            //一键驳回
            else {
                CompanyOther companyOther = new CompanyOther();
                companyOther.setId(messageList.get(a));
                companyOther.setGradeStatus("3");
                companyOther.setAtStatus("3");
                i= iCompanyOtherServer.updateCompanyother(companyOther);
            }
        }



        return i;
}


    /**
     * 驳回操作
     * ids 根据，分割 1.company other 表 主键id  2.companyId
     */
    @GetMapping("/rejectMessage/{id}")
    @Log(title = "驳回合作单位申请", businessType = BusinessType.UPDATE)
    @ResponseBody
    public int rejectMessage(@PathVariable("id") Long id) {

        //更新状态 改为已驳回
        CompanyOther companyOther = new CompanyOther();
        companyOther.setId(id);
        companyOther.setGradeStatus("3");
        companyOther.setAtStatus("3");
        return iCompanyOtherServer.updateCompanyother(companyOther);
    }
}
